package com.jdl.iscm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Lists;
import com.jdl.iscm.entity.enums.StockDimensionEnum;
import com.jdl.iscm.entity.excel.VltExcel;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Ran
 * @Date: 2021/8/20
 **/
@Data
public class VltListener extends AnalysisEventListener<VltExcel> {

    private final List<VltExcel> vltExcelList = Lists.newArrayList();

    @Override
    public void invoke(VltExcel vltExcel, AnalysisContext analysisContext) {

        vltExcelList.add(vltExcel);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        vltExcelList.stream().map(vltExcel -> {
            String dimension = vltExcel.getDimension();
            //如果是全局直接返回，无需处理
            if (StockDimensionEnum.Global.code().equals(dimension)) {
                return vltExcel;
            }
            //如果为非SKU维度设置商品类型；若为SKU维度则设置商品
            String[] arr = vltExcel.getOutInSkuOrNoSku().split("=");
            vltExcel.setOutWh(arr[0]);
            vltExcel.setInWh(arr[1]);
            if (StockDimensionEnum.NoSKU.code().equals(dimension)) {
                vltExcel.setKind(arr[2]);
            }else if (StockDimensionEnum.SKU.code().equals(dimension)) {
                vltExcel.setSku(arr[2]);
            }
            return vltExcel;
        }).collect(Collectors.toList());
    }
}
